define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/order/index' + location.search,
                    add_url: 'goods/order/add',
                    //edit_url: 'goods/order/edit',
                   // del_url: 'goods/order/del',
                    multi_url: 'goods/order/multi',
                    import_url: 'goods/order/import',
                    table: 'goods_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'goods.name', title: __('Goods_id')},
                        {field: 'user.nickname', title: __('User_id')},
                        {field: 'is_coupon', title: __('是否使用优惠券'), searchList: {"0":__('否'),"1":__('是')}, formatter: Table.api.formatter.status},
                        {field: 'out_trade_no', title: __('Out_trade_no'), operate: 'LIKE'},
                        {field: 'price', title: __('Price'), operate:'BETWEEN'},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},

                        {field: 'status', title: __('订单状态'), searchList: {"0":__('未支付'),"1":__('已支付'),"3":__('申请退款'),"4":__('已退款'),"5":__('已完成')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},


                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [

                                {
                                    name: 'click',
                                    title: __('审核'),
                                    text: __('审核'),
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    icon: 'fa fa-leaf',
                                    url: 'goods/order/shenhe',
                                    //visible属性可以让按钮显示或者隐藏，这里通过判断status = 6就显示，否则就隐藏
                                    visible:function(row){
                                        console.log(row);
                                        if(row.status == 3){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                },
                                {
                                    name: 'click',
                                    title: __('核销'),
                                    text: __('销核'),
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    icon: 'fa fa-leaf',
                                    url: 'goods/order/hexiao',
                                    //visible属性可以让按钮显示或者隐藏，这里通过判断status = 6就显示，否则就隐藏
                                    visible:function(row){
                                        console.log(row);
                                        if(row.status == 1){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'goods/order/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'goods/order/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'goods/order/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        shenhe: function () {

            Controller.api.bindevent();
            // Controller.api.shenhesave();
            //取消，支付
            $(document).on('click', '.btn-status-click', function () {
                let order = $(this).data('order');
                let status = order.status;
                let id = order.id;
                $.post('goods/order/shenhe', {
                    id,
                    status,
                    ...order
                }, function (res) {
                    if (res.code == 1) {
                        parent.Toastr.success(res.msg);
                        parent.$(".btn-refresh").trigger("click");
                        parent.layer.closeAll();

                    } else {
                        parent.Toastr.error(res.msg);
                    }
                });
            });
            //驳回
            $(document).on('click', '.btn-statusre-click', function () {
                //let id = $(this).data('id');
                /* var index = parent.layer.getFrameIndex(window.name);
                 parent.layer.close(index);
                 parent.layer.closeAll("refresh");*/
                let id = $("#c-id").val();
                // alert(id);
                parent.Fast.api.open('work/shenhere?ids='+id, __('驳回工单'),{
                    //接收传过来的参数
                    callback:function(value){
                        //在这里可以接收弹出层中使用`Fast.api.close(data)`进行回传数据
                    }
                });
            });
            $(document).on('click', '.btn-statusrere-click', function () {
                parent.layer.closeAll();
            });

        },
        hexiao: function () {

            Controller.api.bindevent();
            // Controller.api.shenhesave();
            //取消，支付
            $(document).on('click', '.btn-statushx-click', function () {
                let order = $(this).data('order');
                let status = order.status;
                let id = order.id;
                $.post('goods/order/hexiao', {
                    id,
                    status,
                    ...order
                }, function (res) {
                    if (res.code == 1) {
                        parent.Toastr.success(res.msg);
                        parent.$(".btn-refresh").trigger("click");
                        parent.layer.closeAll();

                    } else {
                        parent.Toastr.error(res.msg);
                    }
                });
            });
            $(document).on('click', '.btn-statusrere-click', function () {
                parent.layer.closeAll();
            });

        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
